package com.faner.fast.upms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.faner.fast.annotation.SysLog;
import com.faner.fast.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.faner.fast.upms.model.SysDeptDO;
import com.faner.fast.upms.service.SysDeptService;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author faner
 * @since 2021-01-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@Api(value = "dept", tags = "系统-部门管理模块")
@ApiSupport(order = 60)
public class DeptController {

    private final SysDeptService sysDeptService;

    /**
     * 通过ID查询
     * @param id ID
     * @return SysDept
     */
    @ApiOperation(value = "通过ID查询")
    @ApiOperationSupport(order = 10,author = "faner")
    @GetMapping("/dept/detail")
    public R getDeptById(@RequestParam Long id) {
        return R.ok(sysDeptService.getById(id));
    }

    /**
     * 返回树形菜单集合
     * @return 树形菜单
     */
    @ApiOperation(value = "返回树形菜单集合")
    @ApiOperationSupport(order = 20,author = "faner")
    @GetMapping(value = "/dept/tree")
    public R listDeptTrees() {
        return R.ok(sysDeptService.listDeptTrees());
    }

    /**
     * 返回当前用户树形菜单集合
     * @return 树形菜单
     */
    @ApiOperation(value = "返回当前用户树形菜单集合")
    @ApiOperationSupport(order = 30,author = "faner")
    @GetMapping(value = "/dept/user-tree")
    public R listCurrentUserDeptTrees() {
        return R.ok(sysDeptService.listCurrentUserDeptTrees());
    }

    /**
     * 添加
     * @param sysDept 实体
     * @return success/false
     */
    @PreAuthorize("@pms.hasPermission('sys_dept_add')")
    @SysLog("添加部门")
    @ApiOperation(value = "添加部门")
    @ApiOperationSupport(order = 40,author = "faner")
    @PostMapping("/dept/save")
    public R save(@Validated @RequestBody SysDeptDO sysDept) {
        return R.ok(sysDeptService.saveDept(sysDept));
    }


    /**
     * 编辑
     * @param sysDept 实体
     * @return success/false
     */
    @PreAuthorize("@pms.hasPermission('sys_dept_edit')")
    @SysLog("编辑部门")
    @ApiOperation(value = "编辑部门")
    @ApiOperationSupport(order = 50,author = "faner")
    @PostMapping("/dept/update")
    public R update(@Validated @RequestBody SysDeptDO sysDept) {
        return R.ok(sysDeptService.updateDeptById(sysDept));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @PreAuthorize("@pms.hasPermission('sys_dept_del')")
    @SysLog("删除部门")
    @ApiOperation(value = "删除部门")
    @ApiOperationSupport(order = 60,author = "faner")
    @PostMapping("/dept/delete")
    public R removeById(@RequestParam Long id) {
        return R.ok(sysDeptService.removeDeptById(id));
    }


    /**
     * 根据部门名查询部门信息
     * @param deptname 部门名
     * @return
     */
    @ApiOperation(value = "根据部门名查询部门信息")
    @ApiOperationSupport(order = 70,author = "faner")
    @GetMapping("/dept/details/deptname")
    public R getDeptByName(@RequestParam String deptname) {
        SysDeptDO condition = new SysDeptDO();
        condition.setName(deptname);
        return R.ok(sysDeptService.getOne(new QueryWrapper<>(condition)));
    }
}

